<?php
/*
	商品独立佣金设置
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'佣金规则列表'),
	array('op'=>'edit','title'=>'添加/编辑佣金规则'),
);
set_html('tabs',$tabs);
$setting=$this->plugin_setting('areasetting');
$return['levels']=$this->getall('area_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['levels']['0']=array('id'=>0,'title'=>'全部等级');
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('area_goods',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['area_goods']=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('title','image'));
			$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
			//$item['level_id']=$item['level_id']?$this->getcolumn('area_level',array('id'=>$item['level_id']),'title'):'全部等级';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'goods.title','title'=>'商品名称','type'=>'string'),
			array('name'=>'goods.image','title'=>'商品图片','type'=>'image'),
			array('name'=>'ignore','title'=>'是否关闭分销','type'=>'switch'),
			array('name'=>'level_id','title'=>'适用经销商等级','type'=>'select','options'=>'levels'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>18,'action'=>'jy_weishop/goods.get_goods/','name'=>'goods_id','title'=>'选择商品'),
		),
	);
	if($this->setting['open_duodai']){
			$htmls['list']['list'][]=array('type'=>'string','name'=>'commissions.duodai','title'=>'多省代比例（%）');
		}
	for($i=1;$i<=$setting['area_level'];$i++){
		if($i==1){
			$ititle='省';
		}elseif($i==2){
			$ititle='市';
		}elseif($i==3){
			$ititle='县';
		}elseif($i==4){
			$ititle='区';
		}
		$htmls['list']['list'][]=array('type'=>'string','name'=>'commissions.commission'.$i,'title'=>$ititle.'级佣金比例');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'goods_id'=>intval($post['goods_id']),
			'ignore'=>intval($post['ignore']),
			'level_id'=>intval($post['level_id']),
			'commissions'=>$post['commissions']?ijson_encode($post['commissions']):'',
		);
		$id=intval($post['id']);
		if($id){
			$this->update('area_goods',$data,array('id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$this->insert('area_goods',$data);
		}
		exi('设置成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('area_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array('commission1'=>'0.00');
	}else{
		$item=array(
			'title'=>'',
			'commissions'=>array('commission1'=>'0.00'),
		);
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'商品独立佣金规则设置',
		'edit'=>array(
			array('name'=>'goods_id','title'=>'选择商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods/'),
			array('name'=>'ignore','title'=>'是否关闭分销','type'=>'switch','help'=>'关闭后，该商品将不参与分销'),
			array('name'=>'level_id','title'=>'适用等级','type'=>'select','help'=>'','options'=>'levels'),
		),
	);
	if($this->setting['open_duodai']){
		$htmls['edit'][]=array('type'=>'string','name'=>'commissions.duodai','title'=>'多省代比例（%）');
	}
	for($i=1;$i<=$setting['area_level'];$i++){
		if($i==1){
			$ititle='省';
		}elseif($i==2){
			$ititle='市';
		}elseif($i==3){
			$ititle='县';
		}elseif($i==4){
			$ititle='区';
		}
		$htmls['edit'][]=array('type'=>'string','name'=>'commissions.commission'.$i,'title'=>$ititle.'级佣金','help'=>'填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->delete('area_goods',array('id'=>$id));
		}
	}
	exi('删除成功！');
}